function registerSearchAction(id, action, caption) {
	$(id).keydown(function(e){
	    if(e.keyCode == 13)
	    {
	    	e.stopPropagation();
	    	var searchText = $(this).val();
	        console.log("submit search data: " + searchText);
	        SearchDrug.doSearch(action, searchText, caption);
	        return false;
	    }
	});
}

BackboneMVC.namespace('SearchDrug');

SearchDrug.model = new (Backbone.Model.extend({
	url: function() {
		return this.action + "?searchText=" + this.searchText + "&page=1&rows=10";
	}
}))();

SearchDrug.doSearch = function(action, searchText, caption) {
	SearchDrug.model.action = action;
	SearchDrug.model.searchText = searchText;
	SearchDrug.model.fetch({
		error: function(model, message) {
			console.log('Error search drug data for search text: ', model.searchText);
			console.log('Response message: ', message);
			return null;
		},
		success: function(model, json) {
			console.log('Successully search drug data for search text: ', model.searchText);
			console.log('Fetched data: ', json);
			$("#pmsTopForm .pmsCaption").text(caption);
			SearchDrug.getTemplates();
			SearchDrug.renderSearchResults(json);
			return null;
		}
	});
	return null;
};

SearchDrug.templates = {};

SearchDrug.getTemplates = function() {
	return $('script[type="x-handlebars-template"]').each(function() {
		var html, id;
		id = this.id;
		html = $(this).html().trim();
		SearchDrug.templates[id] = Handlebars.compile(html);
		$(this).remove();
		return null;
	});
};

SearchDrug.renderSearchResults = function(json) {
	var index, result, _i, _len;
	$('.pmsPageLeftPanel #body .tableForm tr').remove();
	$('.pmsPageLeftPanel #body .tableForm').addClass("tableFullWidth");
	SearchDrug.renderTotalResult(json.records, '.pmsPageLeftPanel #body .tableForm');
	for (index = _i = 0, _len = json.searchResults.length; _i < _len; index = ++_i) {
		result = json.searchResults[index];
		console.log('Rendering result index: ', index);
		SearchDrug.renderResult(result, '.pmsPageLeftPanel #body .tableForm');
	}
	return null;
};

SearchDrug.renderTotalResult = function(total, renderTo) {
	var $break, html;
	html = SearchDrug.templates['tpl-drug-totalresult']({
		total: total		
	});
	$break = $(html);
	$break.appendTo(renderTo);
	return null;
};

SearchDrug.renderResult = function(result, renderTo) {
	var $break, html;
	html = SearchDrug.templates['tpl-drug-result']({
		id: result.id,
		name: result.name,
		visa: result.visa,
		dispenseType: result.dispenseType,
		manufacturer: result.manufacturer,
		distributor: result.distributor,
		importer: result.importer,
		category: convertDecNCR2CP(result.category),
		subCategory: convertDecNCR2CP(result.subCategory)
	});
	$break = $(html);
	$break.appendTo(renderTo);
	return null;
};